//
//  LXTimeTool.m
//  喜马拉雅改良版框架
//
//  Created by liuxin on 15/12/28.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "LXTimeTool.h"

@implementation LXTimeTool
+(NSString*)getDateWithUpdatedAt:(NSInteger)time{
    NSString *date = @"";
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSDateFormatter *formatterDay = [[NSDateFormatter alloc] init];
    [formatterDay setDateFormat:@"hh"];
//    获取系统当前的时间
//    NSTimeZone**时区是一个地理名字,是为了克服各个地区或国家之间在使用时间上的混乱
    NSDate *liuxin = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate:liuxin];
    NSDate *localDate = [liuxin dateByAddingTimeInterval:interval];
//时间搓转化为NSDate
    NSDate *confromTimesp = [NSDate dateWithTimeIntervalSince1970:time/1000];
//    截取“年-月-日”
    NSString *dataTime = [formatter stringFromDate:localDate];
    NSString *confromTimespStr = [formatter stringFromDate:confromTimesp];
//    只要“时”
    NSString *dataTimeDay = [formatterDay stringFromDate:localDate];
    NSString *conformDay = [formatterDay stringFromDate:confromTimesp];
    
    NSArray *timeArr = [confromTimespStr componentsSeparatedByString:@"-"];
    NSArray *currentArr = [dataTime componentsSeparatedByString:@"-"];
    if ([currentArr[0]integerValue]-[timeArr[0] integerValue] == 0) {
        if ([currentArr[1]integerValue]-[timeArr[1] integerValue] ==0) {
            if ([currentArr[2]integerValue]-[timeArr[2] integerValue] ==0) {
                date = [NSString stringWithFormat:@"%ld小时前",[dataTimeDay integerValue]-[conformDay integerValue]];
            }else{
                date = [NSString stringWithFormat:@"%ld天前",[currentArr[2]integerValue] - [timeArr[2] integerValue]];
            }
        }else{
            date = [NSString stringWithFormat:@"%ld月前", [currentArr[1]integerValue]-[timeArr[1] integerValue]];
        }
    }else{
        date = [NSString stringWithFormat:@"%ld年前",[currentArr[0]integerValue] -[timeArr[0] integerValue]];
    }
    return date;
}
@end
